package org.cc.cloud;

import java.util.Map;

import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;

import org.springframework.messaging.handler.annotation.Header;
import org.springframework.messaging.handler.annotation.Headers;
import org.springframework.messaging.handler.annotation.Payload;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.stereotype.Component;

//这是消费的第三种方式。不在需要写MessageListenerContainer或RabbitListenerConfigurer
@Component
 //可以写在类上面。那么方法上要加@RabbitHandler
//@RabbitListener(queues = {"sms"}, containerFactory="rabbitListenerContainerFactory")
public class SimpleMessageHandle {
//@Payload 表示该字段是内容
//@Headers表示该字段是heads集合
//@Header表示该字段是某个head
	//@RabbitListener(queues = {"sms"})
	public void handle(@Payload String body, @Header(name="filename", required=false) String filename) {
		System.out.println("------------收到消息---------");
		System.out.println(new String(body));
		System.out.println(filename);
	}
	
//	@RabbitListener(queues = {"sms"})v
	public void handle1(@Payload String body, @Header(name="filename", defaultValue="1.png") String filename) {
		System.out.println("------------收到消息---------");
		System.out.println(new String(body));
		System.out.println(filename);
	}

//	@RabbitListener(queues = {"sms"})
	public void handle(@Payload String body, @Headers Map<String, Object> headers) {
		System.out.println("------------收到消息---------");
		System.out.println(new String(body));
		System.out.println(headers);
	}
	
//	@RabbitListener(queues = {"sms"})
	public void handle(@Headers Map<String, Object> headers, @Payload String body) {
		System.out.println("------------收到消息---------");
		System.out.println(new String(body));
		System.out.println(headers);
	}
	
//	@RabbitListener(queues = {"sms"})
	public void handle(byte[] body) {
		System.out.println("------------收到消息---------");
		System.out.println(new String(body));
	}
	
//	@RabbitListener(queues = {"sms"})
	public void handle(String body) {
		System.out.println("------------收到消息(String)---------");
		System.out.println(body);
	}
	
//	@RabbitListener(queues = {"sms"})
//	@RabbitListener(queues = {"weixin", "mail"})
	@RabbitListener(queues = {"${weixin.queueName}", "${mail.queueName}"})//变量方式
	public void handle(Message message) {
		System.out.println("------------收到消息(Message)---------");
		System.out.println(message.getMessageProperties());
		System.out.println(new String(message.getBody()));
	}
	//durable是否持久化
	//自动创建queue 和exchange和routing key并监听
	//@RabbitListener(bindings={@QueueBinding(value=@Queue(value="email", durable="true"), exchange=@Exchange(value="log", durable="true"), key="send")})
	public void declare(Message message) {
		System.out.println("------------收到"+message.getMessageProperties().getConsumerQueue()+"消息---------");
		System.out.println(message.getMessageProperties());
		System.out.println(new String(message.getBody()));
	}
}
